<template>
  <div class="product-card" :class="{ 'is-hovered': isHovered }" @click="goToDetail"
       @mouseenter="isHovered = true" @mouseleave="isHovered = false">
    <!-- 商品图片 -->
    <div class="product-image-container">
      <img
          :src="product.img || '/placeholder-image.jpg'"
          :alt="product.name"
          class="product-image"
      >
      <!-- 品牌标签 -->
      <div class="brand-badge">{{ product.brand || '品牌' }}</div>
    </div>

    <!-- 商品信息 -->
    <div class="product-info">
      <h3 class="product-name">{{ product.name }}</h3>
      <p class="product-description">{{ product.descriptions }}</p>
      <div class="product-price">¥{{ (product.price || 0).toFixed(2) }}</div>
    </div>

    <!-- 悬浮效果元素 -->
    <div class="hover-overlay" v-show="isHovered">
      <button class="add-to-cart-btn">查看详情</button>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';

const props = defineProps({
  product: {
    type: Object,
    required: true,
    default: () => ({})
  }
});

const isHovered = ref(false);
const router = useRouter();

function goToDetail() {
  if (props.product && props.product.id) {
    router.push(`/product/${props.product.id}`);
  }
}
</script>

<style scoped>
.product-card {
  position: relative;
  background: #fff;
  border-radius: 16px;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
  overflow: hidden;
  cursor: pointer;
  height: 100%;
  width: 100%;
  max-width: 260px;
}

.product-card:hover {
  transform: translateY(-8px);
  box-shadow: 0 12px 25px rgba(0, 0, 0, 0.12);
}

.product-image-container {
  position: relative;
  width: 100%;
  height: 180px;
  overflow: hidden;
  border-radius: 12px 12px 0 0;
}

.product-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.5s ease;
}

.product-card:hover .product-image {
  transform: scale(1.05);
}

.brand-badge {
  position: absolute;
  top: 12px;
  left: 12px;
  background-color: rgba(255, 255, 255, 0.9);
  color: #1e40af;
  font-size: 0.75rem;
  font-weight: 600;
  padding: 4px 8px;
  border-radius: 4px;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}

.product-info {
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.product-name {
  font-size: 1rem;
  font-weight: 600;
  color: #333;
  margin: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.product-description {
  font-size: 0.85rem;
  color: #666;
  margin: 0;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
}

.product-price {
  font-size: 1.1rem;
  font-weight: 700;
  color: #e63946;
  margin-top: 4px;
}

.hover-overlay {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  background: linear-gradient(to top, rgba(0, 0, 0, 0.7), transparent);
  padding: 16px 12px 12px;
  display: flex;
  justify-content: center;
  opacity: 0;
  transform: translateY(20px);
  transition: all 0.3s ease;
}

/* 保留CSS悬停效果，确保与Vue状态同步 */
.product-card:hover .hover-overlay {
  opacity: 1;
  transform: translateY(0);
}

.add-to-cart-btn {
  background-color: #1e40af;
  color: white;
  border: none;
  border-radius: 20px;
  padding: 6px 16px;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
  box-shadow: 0 4px 10px rgba(30, 64, 175, 0.2);
}

.add-to-cart-btn:hover {
  background-color: #165DFF;
  transform: translateY(-2px);
  box-shadow: 0 6px 12px rgba(30, 64, 175, 0.3);
}
</style>